﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Configuration;
using SouthernLand.Common.BOL.NhaDat;
using SouthernLand.Common.BOL.User;
using SouthernLand.Common.DAL;
using SouthernLand.Common.Untilities;

namespace SouthernLand.Common
{
    public static class Globals
    {
        public static readonly TheBeerHouseSection Settings =
            (TheBeerHouseSection)WebConfigurationManager.GetSection("landhouse");

        private const string uniChars =
            "àáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ";

        private const string KoDauChars =
            "aaaaaaaaaaaaaaaaaeeeeeeeeeeediiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAAEEEEEEEEEEEDIIIOOOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYAADOOU";

        public static string CurrentUrl
        {
            get
            {
                string strUrl = HttpContext.Current.Request.Url.ToString().Trim();
                return strUrl;
            }
        }

        public static string CurrentUserName
        {
            get
            {
                string userName = "";
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                    userName = HttpContext.Current.User.Identity.Name;
                return userName;
            }
        }

        public static string CurrentUserID
        {
            get
            {
                string userId = string.Empty;
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                    userId = User.GetUserIDByUserName(CurrentUserName);
                return userId;
            }
        }

        public static string FullApplicationPath
        {
            get
            {
                return HttpContext.Current.Request.Url.AbsoluteUri.Replace(
                    HttpContext.Current.Request.Url.PathAndQuery, "") + ApplicationPath;
            }
        }

        public static string ApplicationPath
        {
            get
            {
                string url = HttpContext.Current.Request.ApplicationPath;
                if (url.EndsWith("/"))
                    return url;
                else
                    return url + "/";
            }
        }

        public static string CheckImage(string imageUrl)
        {
            if (imageUrl.Trim().Length <= 3)
                return "Hidden";
            else
                return "";
        }

        public static bool CheckExtensionImage(string imageName)
        {
            string strExtension = Path.GetExtension(imageName).ToUpper();
            if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension == ".PNG" ||
                strExtension == ".SWF")
                return true;
            else
                return false;
        }

        public static bool LangVietNam()
        {
            if (HttpContext.Current.Session["lang"] == "vi-vn" || HttpContext.Current.Session["lang"] == "" ||
                HttpContext.Current.Session["lang"] == null)
                return true;
            else
                return false;
        }

        public static string GetLang(string strVietNam, string strEnglish)
        {
            if (LangVietNam())
                return strVietNam;
            else
                return strEnglish;
        }

        public static string GetGalleryPath()
        {
            return PhysicalApplicationPath() + "//Gallery//";
        }

        public static string ImagesUrl()
        {
            return ApplicationPath + "Images/";
        }

        public static string ImagesUrlAdmin()
        {
            return ApplicationPath + "Admin/Images/";
        }

        public static string ImagesUrlWebMaster()
        {
            return ApplicationPath + "WebMaster/Images/";
        }

        public static string PhysicalApplicationPath()
        {
            return HttpContext.Current.Request.PhysicalApplicationPath;
        }

        public static int GetIntFromQueryString(string key)
        {
            int returnValue = -1;
            string queryStringValue;
            queryStringValue = HttpContext.Current.Request.QueryString[key];
            if (queryStringValue == null)
                return returnValue;
            if (queryStringValue.IndexOf("#") > 0)
                queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

            returnValue = Convert.ToInt32(queryStringValue);
            return returnValue;
        }

        public static string GetStringFromQueryString(string key)
        {
            string returnValue = string.Empty;
            string queryStringValue;
            queryStringValue = HttpContext.Current.Request.QueryString[key];
            if (queryStringValue == null)
                return returnValue;
            if (queryStringValue.IndexOf("#") > 0)
                queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

            returnValue = Convert.ToString(queryStringValue);
            return returnValue;
        }

        public static bool IsDate(string text)
        {
            bool result = true;
            DateTime date;

            try
            {
                date = Convert.ToDateTime(text);
            }
            catch
            {
                result = false;
            }

            return result;
        }

        public static string GenerateRandomCode()
        {
            var random = new Random();
            string s = "";
            for (int i = 0; i < 4; i++)
                s = String.Concat(s, random.Next(10).ToString());
            return s;
        }

        public static string Gia(decimal prices, int unit, string strGiaTren)
        {
            string strValue = string.Empty;
            if (prices > 0)
            {
                strValue = "<span class=Raovat_Gia>" + Formatter.FormatMoney(prices) + "</span> " +
                           DonVT.GetTenDonVTByID(unit);
                strValue = strValue + " " + GiaTren(strGiaTren);
            }
            else
                return "Thương lượng";
            return strValue;
        }

        public static string GiaTren(string strGiaTren)
        {
            if (strGiaTren == "m2")
                return " / m<sup>2</sup>";
            else if (strGiaTren == "thang")
                return " / Tháng";
            else
                return "";
        }

        public static string MetVuong()
        {
            return "<span class=metvuong>m<sup>2</sup></span>";
        }

        public static string LuuTinRao(int raovatId)
        {
            if (raovatId > 0)
            {
                if (CurrentUserID.Length > 5)
                {
                    int count =
                        ClassData.ReturnInteger("Select Count(*) from w4u_RaovatSave Where(RaovatID=" + raovatId +
                                                " And UserID='" + CurrentUserID + "') ");
                    if (count <= 0)
                    {
                        return "<a class=Raovat_Luutin href=" + ApplicationPath + "Raovat/Luutin.aspx?RaovatID=" +
                               raovatId + "&Url=" + CurrentUrl + ">Lưu tin này</a>";
                    }
                    else
                    {
                        return "<span class=Raovat_TinDaLuu>Tin đã được lưu</span>";
                    }
                }
                else
                {
                    return "<a class=Raovat_Luutin href=" + ApplicationPath + "Raovat/Luutin.aspx?RaovatID=" + raovatId +
                           "&Url=" + CurrentUrl + ">Lưu tin này</a>";
                }
            }
            else
                return "";
        }

        public static string UnicodeToKoDau(string s)
        {
            string retVal = String.Empty;
            if (s == null)
                return retVal;
            int pos;
            for (int i = 0; i < s.Length; i++)
            {
                pos = uniChars.IndexOf(s[i].ToString());
                if (pos >= 0)
                    retVal += KoDauChars[pos];
                else
                    retVal += s[i];
            }
            return retVal;
        }

        public static string ProcessString(string s)
        {
            s = RemoveSpecialCharacter(s);
            s = s.Replace(" ", "-");
            s = s.Replace("--", "-");
            s = s.ToLower();
            return s;
        }

        public static string RemoveSpecialCharacter(string s)
        {
            string rgPattern = @"[^A-Za-z0-9 ]";
            Regex oRegex = new Regex(rgPattern);
            s = oRegex.Replace(s, string.Empty);
            return s;
        }

        public static string BuildUrlByNews(int newsId, string newsTitle)
        {
            return String.Format("/newsdetails/{0}/{1}.htm", newsTitle, newsId);
        }

        public static string BuildUrlByZone(int cateId, string cateName)
        {
            return String.Format("/news/{0}/{1}.htm", cateName, cateId);
        }

        public static string BuildUrlByRealEstate(int realId, string realTitle)
        {
            return String.Format("/realestate/{0}/{1}.htm", realTitle, realId);
        }

        public static string BuildUrlByRealEstateDetails(int realDetailId, string realDetailName)
        {
            return String.Format("/realestatedetails/{0}/{1}.htm", realDetailName, realDetailId);
        }

        public static string BuildUrlRssByNews(int distId, DateTime d, string zoneTitle, string distribution, string parent)
        {
            return String.Format("{0}/{1}/{2}.htm", zoneTitle, distribution, distId);
        }

        public static string BuildUrlRssByCate(string zoneName, int zoneid)
        {
            return String.Format("/{0}/{1}.htm", zoneName, zoneid);
        }
    }
}